<?php
/*
 *curl 采集数据函数
 *@param URL string 需要采集的地址
 *@param timeout int 最大采集时间设置
 *@param browser bool 是否模拟浏览器采集
 *@param proxy_ip string 代理ip地址
 *@param proxy_port int 代理ip端口
 *@param expand array 自定义扩展信息 array(‘CURLINFO_CONTENT_TYPE’=>NULL,’CURLINFO_CONTENT_LENGTH_UPLOAD’=>3)
 *return string
 */
function curl_get_file_content($url,$timeout = 30, $browser = false, $proxy_ip = false,$proxy_port = 80 ,$expand = array())
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	if($browser !== false)
	{
		$browser = array(
			'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)',
			'Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0',
			'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)'
		);
		curl_setopt($ch, CURLOPT_USERAGENT, $browser[rand(0, count($browser) - 1)]);
	}
	if($proxy_ip !== false)
	{
		curl_setopt($cUrl, CURLOPT_PROXY, "{$proxy_ip}:{$proxy_port}");
	}
	foreach($expand as $par=>$item)
	{
		curl_setopt($cUrl, $par, $item);
	}
	$info = curl_exec($ch);
	curl_close($ch);
	return $info;
}

function microtime_float()
{
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
}
/*
 * ping 命令 查看网络是否通常
 * @param host ping的地址
 * @param port ping的端口
 * return 失败返回 false 成功 返回 ping时间 单位毫秒
 */
function ping($host,$port = '80')
{
        $time_start = microtime_float();
        $ip = gethostbyname($host);
        $fp = @fsockopen($host,$port,$errno,$errstr,1);
        if(!$fp) return false;
        $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
        @fputs($fp,$get);
        @fclose($fp);
        $time_end = microtime_float();
        $time = $time_end - $time_start;
        $time = ceil($time * 1000);
        return $time;
}